<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="weaver.general.Util"%>
<%@page import="weaver.conn.RecordSet"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.text.ParseException"%>
<%@page import="net.sf.json.JSONObject"%>
<jsp:useBean id="SubCompanyComInfo" class="weaver.hrm.company.SubCompanyComInfo" scope="page" />
<jsp:useBean id="DepartmentComInfo" class="weaver.hrm.company.DepartmentComInfo" scope="page" />
<jsp:useBean id="ResourceComInfo" class="weaver.hrm.resource.ResourceComInfo" scope="page" />
<%@ include file="/systeminfo/init_wev8.jsp" %>
<%
RecordSet rs = new RecordSet();
RecordSet rs1 = new RecordSet();
RecordSet rs2 = new RecordSet();

String weekArr[] = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
String sjd[] = {"上午", "下午", "全天"};
int[] sjdIs = {0,1,2};

int subcompanyid1 = user.getUserSubCompany1();
int userid = user.getUID();

String formid = Util.null2String(request.getParameter("formid"));
String tablename = "";
if(formid.equals("")){
	formid = "-1540";//本周重点安排对应的表单的id
}

String billid = Util.null2String(request.getParameter("billid"));

String formsql = "select * from workflow_bill where id="+formid;
rs.executeSql(formsql);
if(rs.next()){
	tablename = rs.getString("tablename");
}

String type = Util.null2String(request.getParameter("type"));
String rq = Util.null2String(request.getParameter("rq"));
String hdnr = Util.null2String(request.getParameter("field37274"));

String startdate1 = Util.null2String(request.getParameter("startdate1"));
String enddate1 = Util.null2String(request.getParameter("enddate1"));
String from = Util.null2String(request.getParameter("from"));

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date curdate = new Date();

if(rq.equals("")){
	rq = sdf.format(curdate);
}
if(type.equals("0")){
	startdate1 = "";
	enddate1 = "";
	
	curdate = sdf.parse(rq);
}

Calendar calendar=Calendar.getInstance();
int week = curdate.getDay();
if(week==0){
	week = 7;
}
calendar.setTime(curdate);
calendar.add(Calendar.DATE, 1-week);
String week1 = sdf.format(calendar.getTime());
calendar.add(Calendar.DATE, 6);
String week7 = sdf.format(calendar.getTime());

if(startdate1.equals("")){
	startdate1 = week1;
}
if(enddate1.equals("")){
	enddate1 = week7;
}

Date _date = sdf.parse(startdate1);
calendar.setTime(_date);
String weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR)+"";

Map<String, Object> map = new HashMap<String, Object>();
//如果没有选时间段
String sql = "select a.* from "+tablename+" a where (sfktap=1 and ksrq>='"+startdate1+"' and ksrq<='"+enddate1+"') and mcnr like '%"+hdnr+"%'  "+
"order by ksrq,kssj";
//out.print(sql);

rs1.executeSql(sql);
while(rs1.next()){
	Map<String, String> temp = RecordSetToMap(rs1);
	String chrq = Util.null2String(temp.get("ksrq"));//日期
	String jsrq = Util.null2String(temp.get("jsrq"));//结束日期
	
	String sj = temp.get("kssj");//时间，以11点为界限区分上下午	
	String sxw;//上下午或全天
	if(sj.compareTo("11:00:00")<=0){
		sxw = "0";
	}else{
		sxw = "1";
	}
	
	int rq_rows = 2;
	if(map.containsKey(chrq+"_rows")){
		if(map.containsKey(chrq+"_"+sxw+"_rows")){
			rq_rows = Util.getIntValue(""+map.get(chrq+"_rows"));
		}else{
			rq_rows = Util.getIntValue(""+map.get(chrq+"_rows"))-1;
		}
	}
	rq_rows++;
	map.put(chrq+"_rows", rq_rows);
	
	
	List<Map<String, String>> list = new ArrayList<Map<String, String>>();
	int rq_sxw_rows = 0;
	if(map.containsKey(chrq+"_"+sxw)){
		list = (List<Map<String, String>>)map.get(chrq+"_"+sxw);
		rq_sxw_rows = Util.getIntValue(""+map.get(chrq+"_"+sxw+"_rows"));
	}
	rq_sxw_rows++;
	list.add(temp);
	map.put(chrq+"_"+sxw+"_rows", rq_sxw_rows);
	map.put(chrq+"_"+sxw, list);

}
//out.print("<br>"+JSONObject.fromObject(map).toString());


//如果选择时间段
String sql1 = "select a.* from "+tablename+" a where sfktap=0 and mcnr like '%"+hdnr+"%' and ((ksrq>='"+startdate1+"' and jsrq<='"+enddate1+"') "+
"or (ksrq<'"+startdate1+"' and jsrq>='"+startdate1+"' and jsrq<='"+enddate1+"') "+
"or (jsrq>'"+enddate1+"' and ksrq>='"+startdate1+"' and ksrq<='"+enddate1+"') "+
"or (ksrq<'"+startdate1+"' and jsrq>'"+enddate1+"')) "+
"order by ksrq,kssj";
//out.print(sql1);
rs2.executeSql(sql1);//选择跨天后的数据
while(rs2.next()){
	Map<String, String> temp = RecordSetToMap(rs2);
	String chrq = Util.null2String(temp.get("ksrq"));//开始日期
	String jsrq = Util.null2String(temp.get("jsrq"));//结束日期
	String sxw = "2";
	
	String rq_temp = chrq;	
	//如果选择时间段，从时间段的开始时间，到结束时间，一直增加对应的数据
	while(rq_temp.compareTo(jsrq)<=0){	
			
		int rq_rows = 2;
		
		if(map.containsKey(rq_temp+"_rows")){
			if(map.containsKey(rq_temp+"_"+sxw)){//如果已经有全天的数据
				rq_rows = Util.getIntValue(""+map.get(rq_temp+"_rows"));
			}else{
				rq_rows = Util.getIntValue(""+map.get(rq_temp+"_rows"))-1;
			}
		}
		rq_rows++;
		map.put(rq_temp+"_rows", rq_rows);
	
		List<Map<String, String>> list = new ArrayList<Map<String, 	String>>();
		int rq_sxw_rows = 0;
		if(map.containsKey(rq_temp+"_"+sxw)){
			list = (List<Map<String, String>>)map.get(rq_temp+"_"+sxw);
			rq_sxw_rows = Util.getIntValue(""+map.get(rq_temp+"_"+sxw+"_rows"));
		}
		rq_sxw_rows++;
		list.add(temp);
		map.put(rq_temp+"_"+sxw+"_rows", rq_sxw_rows);
		map.put(rq_temp+"_"+sxw, list);

		rq_temp = sdf.format(dayAdd(sdf.parse(rq_temp), "1"));
	}
}
%>
<%!
public String getFirstDayByDate(String date) throws ParseException {
	Calendar calendar=Calendar.getInstance();
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	Date _date = new Date();
	if(!date.equals("")){
		_date = sdf.parse(date);
	}
	calendar.setTime(_date);
	calendar.set(Calendar.WEEK_OF_MONTH, 1);
	
	return sdf.format(calendar.getTime());
}

public String getLastDayByDate(String date) throws ParseException {
	Calendar calendar=Calendar.getInstance();
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	Date _date = new Date();
	if(!date.equals("")){
		_date = sdf.parse(date);
	}
	calendar.setTime(_date);
	calendar.add(Calendar.MONTH, 1);
	calendar.set(Calendar.DATE, 1);
	calendar.add(Calendar.DATE, -1);
	
	return sdf.format(calendar.getTime());
}

public Map<String, String> RecordSetToMap(RecordSet rs){
	Map<String, String> map = null;
	String[] colNames = rs.getColumnName();
	if(colNames!=null && colNames.length>0){
		map = new HashMap<String, String>();
		for(int i=0; i<colNames.length; i++){
			String fieldname = colNames[i];
			String fieldvalue = rs.getString(fieldname);
			map.put(fieldname.toLowerCase(), fieldvalue);
		}
	}
	return map;
}

public Date dayAdd(Date date, String num){
	try{
		Calendar calendar=Calendar.getInstance();
	    calendar.setTime(date);
		calendar.add(Calendar.DATE, Util.getIntValue(num));
		
		return calendar.getTime();
	}catch (Exception e) {
		return new Date();
	}
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
<SCRIPT language="javascript" src="/js/weaver_wev8.js"></script>
<SCRIPT language="javascript" src="/js/weaverTable_wev8.js"></script>
<SCRIPT type="text/javascript" src="/formmode/js/WdatePicker/WdatePicker_wev8.js"></script>
<LINK REL=stylesheet type=text/css HREF=/css/Weaver_wev8.css>
<link href="/formmode/css/formmode_wev8.css" type="text/css" rel="stylesheet" />
<script language=javascript src="/wui/common/jquery/plugin/Listener_wev8.js"></script>
<SCRIPT type="text/javascript" src="/js/workflow/VCEventHandle_wev8.js"></SCRIPT>
<script src='echarts.min.js'></script>
<script src='macarons.js'></script>
<style type="text/css">
*{
	margin: 0;
	padding: 0;
	border: 0;
	list-style-type: none;
	font-size: 14px;
}
td,th{
	border-bottom: 1px solid gray;
	border-right: 1px solid gray;
	height: 30px;
	text-align: center;
}
th{
	font-weight: bold;
}
.btn{
	height:30px;
	width: 1100px;
	border: 1px solid black;
	background-color: white;
}
#tablecontainer,#tablecontainer1{ 
	width: 100%; 
	height: 400px; 
	margin: 0 auto; 
}
a{
	color: #018efb;
}
a:hover {
	text-decoration: underline !important;
}
</style>
<script type="text/javascript">
function onSearchWFQTDate(spanname,inputname){
	var oncleaingFun = function(){
		  $(spanname).innerHTML = '';
		  inputname.value = '';
		}
		WdatePicker({el:spanname,onpicked:function(dp){
			var returnvalue = dp.cal.getDateStr();
			$dp.$(inputname).value = returnvalue;
		},oncleared:oncleaingFun});
}

var temp_sd = "<%=startdate1 %>"; 
var temp_ed = "<%=enddate1 %>";
var temp_ws = 0;

jQuery(document).ready(function(){
  //temp_ws = getws();
  //temp_ws = getws();
  //var date = new Date();
  //var week = date.getDay();
  //temp_sd = get(date, 1-week);
  //date =new Date(temp_sd);
  //temp_ed = get(date, 7);
  
  //setIfr(0);
});

function setIfr(type){
  if(type==0){
    temp_ws = getws();
    var date = new Date();
    var week = date.getDay();
    temp_sd = get(date, 1-week);
    date =new Date(temp_sd);
    temp_ed = get(date, 6);
  }else if(type==-1){
    temp_ws = temp_ws-1;
    var date =new Date(temp_sd);
    temp_sd = get(date, -7);
    date =new Date(temp_sd);
    temp_ed = get(date, 6);
  }else if(type==1){
    temp_ws = temp_ws+1;
    var date =new Date(temp_sd);
    temp_sd = get(date, 7);
    date =new Date(temp_sd);
    temp_ed = get(date, 6);
  }
  
  //alert(temp_ws+"--"+temp_sd+"--"+temp_ws);
  window.location.href = "yzgzap_gxj.jsp?formid=<%=formid%>&type=1&startdate1="+temp_sd+"&enddate1="+temp_ed;
}

function getws(){
  var today = new Date();
  var firstDay = new Date(today.getFullYear(),0, 1);
  var dayOfWeek = firstDay.getDay(); 
  var spendDay= 1;
  if (dayOfWeek !=0) {
    spendDay=7-dayOfWeek+1;
  }
  firstDay = new Date(today.getFullYear(),0, 1+spendDay);
  var d =Math.ceil((today.valueOf()- firstDay.valueOf())/ 86400000);
  var result =Math.ceil(d/7);
  return result+1;
}

function get(date, n){
	date.setDate(date.getDate() + n);
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var day = date.getDate();
	if(month<10){
		month = "0"+month;
	}
	if(day<10){
		day = "0"+day;
	}
	return year+"-"+month+"-"+day;
}

function doExcel(){
	var startdate1 = "<%=startdate1%>";
	var enddate1 = "<%=enddate1%>";
	window.location.href = "yzgzap_gxj_excel.jsp?formid=<%=formid%>&startdate1="+temp_sd+"&enddate1="+temp_ed;
}

function doSearch(){
	var rq = jQuery("#rq").val();
	var hdnr = jQuery("#hdnr").val();
	window.location.href = "yzgzap_gxj.jsp?formid=<%=formid%>&type=0&rq="+rq+"&field37274="+hdnr;
}

function doCreate(){
	window.open("/spa/cube/index.html#/main/cube/card?type=1&modeId=1113&formId=-1540");
}

//弹出框方法
function showDialog(url){
	var diaHeight = $(document).height()*0.6;
	var diaWidth = $(document).width()*0.6;
 	dialog = new top.Dialog();
 	dialog.currentWindow = window;
 	dialog.Drag = true;
 	dialog.Width = diaWidth;
 	dialog.Height = diaHeight;
 	dialog.URL = url;
	dialog.show();
}

/*function _refresh(){
	window.location.reload();
}*/

/*没用上
//单击事件，弹出编辑模板，type=2
function doClick(){	
	showDialog("/spa/cube/index.html#/main/cube/card?type=2&modeId=1113&formId=-1540");//此处的连接后必须加上&billid=模块表单id，否则无法打开对应的编辑模板
}

//双击事件，弹出新建模板，type=1
function doDoubleClick(){
    showDialog("/spa/cube/index.html#/main/cube/card?type=1&modeId=1113&formId=-1540");
}
*/
</script>
  </head>
  
  <body>
<table cellpadding="0" cellspacing="0" border="0" style="width: 100%;border-top: 1px solid gray;border-left: 1px solid gray;">
<%
if(!from.equals("portal")){
%>
	<tr>
		<th colspan="<%=4 %>" style="text-align: left;padding-left: 20px;border-right: 0;">
			日期：<button class="calendar" onclick="onSearchWFQTDate(rq_span,rq)" type="button"></button>
				<input type="hidden" class="calendar" id="rq" name="rq" onclick="WdatePicker()" value="<%=rq %>"/>
				<span id="rq_span" name="rq_span"><%=rq %></span>
			活动内容：<input type="text" id="hdnr" name="hdnr" value="<%=hdnr %>"/>
			<input type="button" value="查询" onclick="doSearch()" class="middle e8_btn_top_first" style="margin-left: 20px;" />
		</th>
		<th colspan="<%=3 %>" style="text-align: right;padding-left: 20px;">
			<input type="button" value="新建记录" onclick="doCreate()" class="middle e8_btn_top_first" style="margin-right: 100px;" />
			<input type="button" value="上一周" onclick="setIfr(-1)" class="middle e8_btn_top_first" style="margin-right: 20px;" />
			<input type="button" value="本周" onclick="setIfr(0)" class="middle e8_btn_top_first" style="margin-right: 20px;" />
			<input type="button" value="下一周" onclick="setIfr(1)" class="middle e8_btn_top_first" style="margin-right: 20px;" />
			<input type="button" value="导出" onclick="doExcel()" class="middle e8_btn_top_first" style="margin-right: 100px;" />
		</th>
	</tr>
	<tr>
		<th colspan="<%=7 %>" style="font-size: 20px;height: 50px;">
			一周主要工作安排第（<%=weekOfYear %>）周 <%=startdate1 %> ~ <%=enddate1 %>
		</th>
	</tr>
<%
}
%>
	<tr style="background-color: #c4e2f9;">
		<th style="width: 8%;" colspan="2">日期</th>
		<th style="width: 20%;">活动内容</th>
		<th style="width: 10%;">参与领导</th>
		<th style="width: 10%;">责任部门</th>
		<th style="width: 10%;">责任人</th>
		<th style="width: 10%;">地点</th>
	</tr>
<%
String _rq = startdate1;
while(_rq.compareTo(enddate1)<=0){
	int rq_index = 0;	
	int rq_rows = Util.getIntValue(Util.null2String(map.get(_rq+"_rows")));
		
	List<Map<String, String>> list_rq = (List<Map<String, String>>)map.get(_rq);
	
	Date ksrqDate = sdf.parse(_rq);
	String weekStr = weekArr[ksrqDate.getDay()];
	String __rq = _rq.substring(5);
	__rq = __rq.replace("-","月")+"日";
	
	if(rq_rows<=0){
	%>
	<tr>
		<td rowspan=3 ><%=__rq+"<br>"+"("+weekStr+")" %></td>
		<td>上午</td>
		<td ondblclick="showDialog('/spa/cube/index.html#/main/cube/card?type=1&modeId=1113&formId=-1540&field37275=1&field37277=09:00&field37276=<%=_rq%>')"></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
	</tr>
	<tr>
		<td>下午</td>
		<td ondblclick="showDialog('/spa/cube/index.html#/main/cube/card?type=1&modeId=1113&formId=-1540&field37275=1&field37277=14:00&field37276=<%=_rq%>')"></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
	</tr>
	<tr>
		<td>全天</td>
		<td ondblclick="showDialog('/spa/cube/index.html#/main/cube/card?type=1&modeId=1113&formId=-1540&field37275=0&field37277=09:00&field37276=<%=_rq%>')"></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
	</tr>
	<%
	}else if(rq_rows>0){
		for(int i : sjdIs){
			int rq_sxw_rows = Util.getIntValue(Util.null2String(map.get(_rq+"_"+i+"_rows")));
			int rq_sxw_index = 0;
			if(rq_sxw_rows<=0){
				rq_index++;
				rq_sxw_index++;
			%>
			<tr>
				<%if(rq_index==1){ %><td rowspan="<%=rq_rows %>"><%=__rq+"<br>"+"("+weekStr+")" %></td><%} %>
				<%if(rq_sxw_index==1){ %><td><%=sjd[i] %></td><%} %>
				<%if(i==0){ %><td ondblclick="showDialog('/spa/cube/index.html#/main/cube/card?type=1&modeId=1113&formId=-1540&field37275=1&field37277=09:00&field37276=<%=_rq%>')"></td><%}else if(i==1){%><td ondblclick="showDialog('/spa/cube/index.html#/main/cube/card?type=1&modeId=1113&formId=-1540&field37275=1&field37277=14:00&field37276=<%=_rq%>')"></td><%}else if(i==2){%><td ondblclick="showDialog('/spa/cube/index.html#/main/cube/card?type=1&modeId=1113&formId=-1540&field37275=0&field37277=09:00&field37276=<%=_rq%>')"></td><%}	%>			
				<td></td>
				<td></td>
				<td></td>
				<td></td>
			</tr>
	<%
			}else if(rq_sxw_rows>0){
				List<Map<String, String>> list = (List<Map<String, String>>)map.get(_rq+"_"+i);
				for(Map<String, String> temp : list){
					rq_index++;
					rq_sxw_index++;
					
					String sfktap = Util.null2String(temp.get("sfktap"));//是否跨天安排37275
					String sj = Util.null2String(temp.get("kssj"));//开始时间37277
					String sj_temp = sj.substring(0,5);
					if(sfktap.equals("0")){
						sj_temp = "全天";
					}
					String jdnr = Util.null2String(temp.get("mcnr"));//活动内容37274
					String zrr = Util.null2String(temp.get("zrr"));//责任人37282
					String zrbm = Util.null2String(temp.get("zrbm"));//责任部门37281
					String cyld = Util.null2String(temp.get("cyld"));//参与领导37283
					String dd = Util.null2String(temp.get("dd"));//地点37280
					
					String id = Util.null2String(temp.get("id"));
					String ksrq = Util.null2String(temp.get("ksrq"));//开始日期37276
					String jsrq = Util.null2String(temp.get("jsrq"));//结束日期37278
					String jssj = Util.null2String(temp.get("jssj"));//结束时间37279
%>
	<tr>
		<%if(rq_index==1){ %><td rowspan="<%=rq_rows %>"><%=__rq+"<br>"+"("+weekStr+")" %></td><%} %>
		<%if(rq_sxw_index==1){ %><td rowspan="<%=rq_sxw_rows %>"><%=sjd[i] %></td><%} %>		
		<%if(i==0){ %><td style="text-align: left;" ondblclick="showDialog('/spa/cube/index.html#/main/cube/card?type=2&modeId=1113&formId=-1540&field37275=1&field37277=09:00&field37276=<%=_rq%>')"><a onclick="showDialog('/spa/cube/index.html#/main/cube/card?type=0&modeId=1113&formId=-1540&billid=<%=id%>')"><%=sj_temp+"&emsp;"+jdnr %></a></td><%}else if(i==1){%><td style="text-align: left;" ondblclick="showDialog('/spa/cube/index.html#/main/cube/card?type=2&modeId=1113&formId=-1540&field37275=1&field37277=14:00&field37276=<%=_rq%>')"><a onclick="showDialog('/spa/cube/index.html#/main/cube/card?type=0&modeId=1113&formId=-1540&billid=<%=id%>')"><%=sj_temp+"&emsp;"+jdnr %></a></td><%}else if(i==2){%><td style="text-align: left;" ondblclick="showDialog('/spa/cube/index.html#/main/cube/card?type=2&modeId=1113&formId=-1540&field37275=0&field37277=09:00&field37276=<%=_rq%>')"><a onclick="showDialog('/spa/cube/index.html#/main/cube/card?type=0&modeId=1113&formId=-1540&billid=<%=id%>')"><%=jdnr %></a></td><%} %>	
		<td><%=ResourceComInfo.getLastnames(cyld) %></td>
		<td><%=DepartmentComInfo.getDepartmentName(zrbm) %></td>
		<td><%=zrr %></td>
		<td><%=dd %></td>
	</tr>
<%
				}
			}
		}
	}
	_rq = sdf.format(dayAdd(sdf.parse(_rq), "1"));
}
%>
</table>
  </body>
</html>
